package me.timlong.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;


/**
 * 当Spring容器内没有TomcatEmbeddedServletFactory这个Bean时，会把次bean加载到Spring容器内
 */

@Component
public class WebServiceConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    // 在application文件配置的基础上，补充配置的相关tomcat容器的参数配置

    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        // 使用对应工厂类提供给我们的接口定制化我们的tomcat connector
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

                // 定值化keepAliveTimeOut  => 30s
                protocol.setKeepAliveTimeout(30000);
                // 当客户端发送查过10000个请求则自动断开keepalive请求
                protocol.setMaxKeepAliveRequests(10000);


            }
        });

    }
}
